<?php

namespace loggedin\view;

require_once("/loggedin/model/Question.php");

class QuestionView {
	private static $addQuestion = "addquestion";
	private static $createQuestion = "createQuestion";
	private static $question = "question";
	private static $rightAnswer = "rightAnswer";
	private static $wrongAnswer1 = "wrongAnswer1";
	private static $wrongAnswer2 = "wrongAnswer2"; 
	private static $wrongAnswer3 = "wrongAnswer3";
	private static $myquestions = "myquestions";
	private static $alterQuestionID = "alterquestionid";
	private static $alterQuestion = "alterQuestion";
	private static $errormessage = "errormessage";

	/**
	 * @param  string HTML $questionsHTML
	 * @return string HTML
	 */
	public function getCreateQuestionPage($questionsHTML) {
		$message = "";
		if(isset($_COOKIE[self::$errormessage])) {
			$message = $_COOKIE[self::$errormessage];
		}
		return "<!DOCTYPE HTML SYSTEM
		<html>
			<head>
				<title>Quizzer inloggad</title>
				<meta charset='UTF-8'>
			</head>
			<body>
				<div class='wrapper'>
					<div class='leftBox'>
						<h1>Quizzer</h2>
						<p><a href='?'>Tillbaka</a></p>
					</div>

					<div class='mainContent'>
						<p>" . $message . "</p>
						<form action='?createQuestion' method='post'>
							<h2>Lägg till fråga</h2>
							<lable for='question'>Fråga: </lable>
							<textarea id='question' name='" . self::$question . "'></textarea><br />
							<lable for='rightAnswer'>Rätt svar: </lable>
							<input class='add' type='text' id='rightAnswer' name='" . self::$rightAnswer ."'> <br />
							<lable for='wrongAnswer1'>Fel svar 1: </lable>
							<input class='add' type='text' id='wrongAnswer1' name='" . self::$wrongAnswer1 ."'> <br />
							<lable for='wrongAnswer2'>Fel svar 2: </lable>
							<input class='add' type='text' id='wrongAnswer2' name='" . self::$wrongAnswer2 ."'> <br />
							<lable for='wrongAnswer3'>Fel svar 3: </lable>
							<input class='add' type='text' id='wrongAnswer3' name='" . self::$wrongAnswer3 ."'> <br />
							<input class='button' type='submit' value='Lägg till fråga'>	
						</form>
						$questionsHTML
					</div>
				</div>
			</body>
		</html>";
	}

	/**
	 * @param  string HTML $questionsHTML
	 * @return string HTML
	 */
	public function getMyQuestionPage($questionsHTML) {
		return "<!DOCTYPE HTML SYSTEM>
		<html>
			<head>
				<title>Quizzer inloggad</title>
				<meta charset='UTF-8'>
			</head>
			<body>
				<div class='wrapper'>
					<div class='leftBox'>
						<h1>Quizzer</h1>
						<a href='?'>Tillbaka</a>
					</div>
					<div class='mainContent'>
						<br>
						$questionsHTML
					</div>
				</div>
			</body>
		</html>";
	}

	/**
	 * @param  loggedin\model\Question $question
	 * @return string HTML
	 */
	public function getAlterQuestionPage(\loggedin\model\Question $question) {
		return "<!DOCTYPE HTML SYSTEM>
		<html>
			<head>
				<title>Quizzer inloggad</title>
				<meta charset='UTF-8'>
			</head>

			<body>
				<div class='wrapper'>
					<div class='leftBox'>
						<h1>Quizzer</h1>
						<a href='?'>Tillbaka</a>
					</div>
					<div class='mainContent'>
						<form action='?" . self::$alterQuestion . "' method='post'>
							<h2>Ändra fråga</h2>
							<lable for='question'>Fråga: </lable>
							<textarea id='question' name='" . self::$question ."'>" . 
										$question->getQuestion() . "</textarea> <br />
							<lable for='rightAnswer'>Rätt svar: </lable>
							<input class='add' type='text' id='rightAnswer' name='" . self::$rightAnswer .
								"' value='" . $question->getRightAnswer() . "'> <br />
							<lable for='wrongAnswer1'>Fel svar 1: </lable>
							<input class='add' type='text' id='wrongAnswer1' name='" . self::$wrongAnswer1 .
								"' value='" . $question->getWrongAnswer1() . "'> <br />
							<lable for='wrongAnswer2'>Fel svar 2: </lable>
							<input class='add' type='text' id='wrongAnswer2' name='" . self::$wrongAnswer2 .
								"' value='" . $question->getWrongAnswer2() . "'> <br />
							<lable for='wrongAnswer3'>Fel svar 3: </lable>
							<input class='add' type='text' id='wrongAnswer3' name='" . self::$wrongAnswer3 .
								"' value='" . $question->getWrongAnswer3() . "'> <br />
							<input class='button' type='submit' value='Ändra fråga'>
						</form>
					</div>
				</div>
			</body>
		</html>";
	}

	/**
	 * @return boolean 
	 */
	public function getChangeQuestionInfo() {
		return isset($_GET[self::$alterQuestion]);
	}

	public function setAddQuestionSuccessMessage() {
		$this->message = "Fråga tillagd.";
	}

	public function setChangeQuestionSuccessMessage() {
		$this->message = "Frågan har ändrats.";
	}

	/**
	 * @param int $questionID
	 */
	public function setAlterQuestionIDCookie($questionID) {
		setcookie(self::$alterQuestionID, $questionID, time()+3600);
	}

	public function removeAlterQuestionIDCookie() {
		setcookie(self::$alterQuestionID, 0, time()-3600);
	}

	/**
	 * @return string 
	 */
	public function getAlterQuestionIDCookie() {
		return $_COOKIE[self::$alterQuestionID];
	}

	public function removeErrorMessage() {
		setcookie(self::$errormessage, "", time()-3600);
	}
	
	public function setErrorMessage($message) {
		if($message == "1") {
			setcookie(self::$errormessage, "Frågan är för kort för att godkännas. Minst 20 tecken.");
		}
		else if($message == "2") {
			setcookie(self::$errormessage, "Det rätta svaret är för kort för att godkännas. Minst 1 tecken.");
		}
		else if($message == "3") {
			setcookie(self::$errormessage, "Fel svar 1 är för kort för att godkännas. Minst 1 tecken.");
		}
		else if($message == "4") {
			setcookie(self::$errormessage, "Fel svar 2 är för kort för att godkännas. Minst 1 tecken.");
		}
		else if($message == "5") {
			setcookie(self::$errormessage, "Fel svar 3 är för kort för att godkännas. Minst 1 tecken.");
		}
		else if($message == "6") {
			setcookie(self::$errormessage, "Frågan innehåller taggar och kan inte godkännas.");
		}
	}
	/**
	 * @return boolean 
	 */
	public function getCreateQuestionInfo() {
		return isset($_GET[self::$addQuestion]);
	}
	/**
	 * @return boolean 
	 */
	public function getAddQuestionInfo() {
		return isset($_GET[self::$createQuestion]);
	}
	/**
	 * @return \loggedin\model\Question
	 */
	public function getNewQuestionInfo() {
		$question = $_POST[self::$question];
		$rightAnswer = $_POST[self::$rightAnswer];
		$wrongAnswer1 = $_POST[self::$wrongAnswer1];
		$wrongAnswer2 = $_POST[self::$wrongAnswer2];
		$wrongAnswer3 = $_POST[self::$wrongAnswer3];

		return new \loggedin\model\Question(0, $question, $rightAnswer, $wrongAnswer1,
											$wrongAnswer2, $wrongAnswer3);
	}

	/**
	 * @param  loggedin\model\Question $question
	 * @return string HTML
	 */
	public function questionToHTML(\loggedin\model\Question $question) {
		$ret = "<div class='question'>";
		$ret .= "<p>";
		$ret .= "Fråga nummer: " . $question->getQuestionID() . "<br>";
		$ret .= "Fråga: " . $question->getQuestion() . "<br>";
		$ret .= "Rätt svar: " . $question->getRightAnswer() . "<br>";
		$ret .= "Fel svar 1: " . $question->getWrongAnswer1() . "<br>";
		$ret .= "Fel svar 2: " . $question->getWrongAnswer2() . "<br>";
		$ret .= "Fel svar 3: " . $question->getWrongAnswer3() . "<br>";
		$ret .= "<a href='?" . self::$alterQuestionID . "=" . $question->getQuestionID() . "'>Ändra fråga</a>";
		$ret .= "</p>";
		$ret .= "</div>";
		return $ret;
	}
	/**
	 * @return boolean 
	 */
	public function getAlterQuestionInfo() {
		return isset($_GET[self::$alterQuestionID]);
	}
	/**
	 * @return string 
	 */
	public function getAlterQuestionIDValue() {
		return $_GET[self::$alterQuestionID];
	}
	/**
	 * @param  \loggedin\model\Question $questions
	 * @return string HTML
	 */
	public function getQuestionsToHTML($questions) {
		$ret = "";
		foreach ($questions as $question) {
			$ret .= $this->questionToHTML($question);
		}
		return $ret;
	}

	/**
	 * @return boolean 
	 */
	public function getMyQuestionsInfo() {
		return isset($_GET[self::$myquestions]);
	}
}